<template>
    <div class='content'>
        <div class="profile_page">
                <head-top  :go-back='goBack'  :head-title="profiletitle"></head-top>
        </div>
        <div class="formContain">
            <titlebar :title-left='titleLeft'></titlebar>
            <ul>
                <li class="flex">
                    <span class="label"><i class="require">*</i>补打卡日期</span>
                    <div @click="showdatepicker($event,'suppDate')">
                        <input type="text"  id="begin" placeholder="请选择时间" :value="datas.suppDate"   readonly>
                        <img src="@/assets/images/img/calendarIcon.png" alt="" srcset="" >
                    </div>
                </li>
              <van-radio-group v-model="datas.cardType" @change="radioClick">
                <li class="flex forget" v-if="!ifFourCard">
                    <span class="label" style="padding-left:1em;">上班卡</span>
                    <div style="padding-right:2rem;">
                        <van-radio name="3221000001"  style="height:24px;align-items: flex-start;"></van-radio>
                    </div>
                </li>
                 <li class="flex forget" v-if="!ifFourCard">
                    <span class="label"  style="padding-left:1em;">下班卡</span>
                    <div style="padding-right:2rem;">
                        <van-radio name="3221000002"  style="height:24px;align-items: flex-start"></van-radio>
                    </div>
                </li>
                 <li class="flex forget" v-if="ifFourCard">
                    <span class="label" style="padding-left:1em;">第一次签到</span>
                    <div style="padding-right:2rem;">
                        <van-radio name="oneWorkIn"  style="height:24px;align-items: flex-start;"></van-radio>
                    </div>
                </li>
                 <li class="flex forget" v-if="ifFourCard">
                    <span class="label"  style="padding-left:1em;">第一次签退</span>
                    <div style="padding-right:2rem;">
                        <van-radio name="oneWorkOut"  style="height:24px;align-items: flex-start"></van-radio>
                    </div>
                </li>
                 <li class="flex forget" v-if="ifFourCard">
                    <span class="label" style="padding-left:1em;">第二次签到</span>
                    <div style="padding-right:2rem;">
                        <van-radio name="twoWorkIn"  style="height:24px;align-items: flex-start;"></van-radio>
                    </div>
                </li>
                 <li class="flex forget" v-if="ifFourCard">
                    <span class="label"  style="padding-left:1em;">第二次签退</span>
                    <div style="padding-right:2rem;">
                        <van-radio name="twoWorkOut"  style="height:24px;align-items: flex-start"></van-radio>
                    </div>
                </li>
                </van-radio-group>
                  <li class="flex">
                    <span class="label">补打卡时间</span>
                   <!-- <div @click="showdatepicker($event,'end')">
                        <input type="text"  id="end" placeholder="请选择时间" :value="datas.end"  readonly>
                        <img src="@/assets/images/img/calendarIcon.png" alt="" srcset="" >
                    </div>-->
                    <div>
                      <input type="text"  id="end" :value="datas.suppTime"  readonly>
                     <!-- <img src="@/assets/images/img/calendarIcon.png" alt="" srcset="" >-->
                    </div>
                </li>
<!--                <li class="flex">
                    <span class="label" style="padding-left:1em;">上班卡</span>
                    <div style="padding-right:2rem;">
                        <van-checkbox v-model="checked" style="height:24px;align-items: flex-start;"></van-checkbox>
                    </div>
                </li>
                 <li class="flex">
                    <span class="label" style="padding-left:1em;">下班卡</span>
                    <div style="padding-right:2rem;">
                        <van-checkbox v-model="checked" style="height:24px;align-items: flex-start;"></van-checkbox>
                    </div>
                </li>-->
            </ul>

        </div>
         <div class="formContain">
            <titlebar :title-left='titleLeft1'></titlebar>
            <ul>
                <li class="flex">
                    <div class='full_width' style="min-height:2rem">
                      <i class="require">*</i>
                        <van-field class='full_width'  rows="1" autosize v-model="datas.remark" placeholder="请输入补卡原因" type="textarea" input-align="left"/>
                    </div>
                </li>
                <li class="flex">
                    <span class="label">上传附件</span>
                    <div>
                      <input type="text" v-model="fileName" disabled style="background: transparent;width:10rem;position: absolute;padding-right: 1rem">
                      <van-uploader :after-read="afterRead" :before-read="beforeRead" id="file">
                        <span class="iconfont icon-hao"></span>
                      </van-uploader>
                    </div>
                </li>
            </ul>
         </div>
         <div class="align_center foot-button" >
              <van-button class="cancel-btn" type="primary"  @click="$router.go(-1)">取消</van-button>
            <van-button class="primary-btn" type="primary" @click="saveClock">提交</van-button>
         </div>
        <van-picker id="picker"   show-toolbar  :title="title" v-show='pickershow' :columns="columns" @cancel="onCancel" @confirm="onConfirm" />
        <van-datetime-picker v-show='show' id="datetime-picker" type="date" @confirm="datetimeSelect" @cancel='cancel' v-model="currentTime" />
    </div>
</template>

<script>
    import headTop from '@/components/header/head'
     import titlebar from '@/components/titlebar3/titlebar3';
    import {canSave,saveActForgetClock,startProcess,processInstance,getWorkTime,uploadFile,selfPersonInfo,getWorkTimeType} from '@/service/getData';
    import {getStore,getDefault} from '@/assets/config/mUtils';
    import { Dialog ,Toast} from 'vant';
    export default {
    	data(){
            return{
                profiletitle:'忘打卡',
                goBack:true,
                titleLeft:'补打卡信息',
                titleLeft1:'补卡原因',
                checked:false,
                show:false, //控制日期显示隐藏
                pickershow:false,//控制下拉选显示隐藏
                pickerName:'',//当前控制日期的控件
                currentTime:new Date(),
                startManagerId:'',
                fileName:'',
               screenHeight:'',
               ifFourCard:false,
                bussinessTable:'ACT_ATND_SUPPLEMENT_CARD',
                datas:{
                  suppDate:'',
                  suppTime:'',
                  cardType:'',
                  remark:'',
                  attachmentId:"ffwef34355335419DBE21C52C3E27BF4A"

                },
                title:'标题',//下拉框标题
                columns: ['杭州', '宁波', '温州', '嘉兴', '湖州'] , //下拉框数据
                orgType:'',
            }
        },
        created(){
          var processData = {};
          processData.processDefinitionKey = 'supplementCardProcess';
          processData.app_userId = getStore('user_id');
          processData.CHECK_TIME = new Date().getTime().toString();
          processData.CHECK_SIGN = this.$md5(processData.app_userId + processData.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
          // processData = JSON.stringify(processData);
          startProcess(processData).then(res=>{
            console.log(res)
            this.startManagerId = res.data.startManagerId;
          });
          var actData = {};
          actData.processRespondentUserId =  getStore('user_id');
          actData.app_userId = getStore('user_id');
          actData.CHECK_TIME = new Date().getTime();
          actData.CHECK_SIGN = this.$md5(actData.app_userId + actData.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
          selfPersonInfo(actData).then(res=>{
            this.orgType = res.data.org;
          })
        },
        mounted(){
          this.screenHeight = document.documentElement.clientHeight;
        },
      updated () {
        let that = this;
        this.$nextTick(function () {
          let $inputs = Array.from(document.getElementsByTagName('input'));
          /*let body = document.body;
          $inputs.forEach(item => { // 定时器是处理多个input间快速切换闪屏问题，可省略
              item.onblur = function () { // onblur是核心方法
                  clearTimeout(body.timer);
                  body.timer = setTimeout(() => {
                  window.scroll(0, 0);
                  window.innerHeight = window.outerHeight = that.screenHeight
                  }, 150)
              }
              item.onfocus = function () {
                  clearTimeout(body.timer)
              }
          })*/
          let body = document.body;
          $inputs.forEach(item => {
            item.onblur = function () { // onblur是核心方法
              window.scroll(0, 0);
              window.innerHeight = window.outerHeight = that.screenHeight;
              //console.log(that.screenHeight);
            }
          })
        })
      },
        components:{
            headTop,
            titlebar
        },
        methods:{
            addZero(num){
              var addNum = num<10?"0"+num:num;
              return addNum;
            },
            getWorkTimes(){
              var data = {};
              data.personId = getStore('user_id');
              data.app_userId = getStore('user_id');
              data.CHECK_TIME = new Date().getTime().toString();
              data.CHECK_SIGN = this.$md5(data.app_userId + data.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
              var forgetDate =  new Date(this.datas.suppDate);
              var val = forgetDate.getFullYear() + '-'+ this.addZero(forgetDate.getMonth()+1) + '-'+ this.addZero(forgetDate.getDate());
              data.atndDate = val;
              data.cardType = this.datas.cardType;
              getWorkTime(data).then(res=>{
                console.log(res,"补打卡");
                if(res.code == 400){
                  Dialog.alert({
                    title: '提示',
                    message: res.msg
                  });
                }
                this.datas.suppTime = res.data.suppTime;
              })
            },
            datetimeSelect(value){
                this.show = false;
                var date =  new Date(value);
              var val = date.getFullYear() + '-'+this.addZero(date.getMonth()+1) + '-'+ this.addZero(date.getDate());
                this.$set(this.datas,this.pickerName,val);
                getWorkTimeType({'atndDate':val,'personId':getStore('user_id')}).then(res =>{
                  if(res.code=='200'){
                    this.ifFourCard = res.data.ifFourCard
                  }else{
                     Dialog.alert({
                        title: '提示',
                        message: res.msg
                      });
                  }
                  
                })
                // document.getElementById(this.pickerName).value = val;
                if(this.datas.cardType!=''&& this.datas.suppDate!=''){
                  this.getWorkTimes()
                }
            },
            afterRead(file) {
              // 此时可以自行将文件上传至服务器
              Toast.loading({
                duration: 0,   // 持续展示 toast
                mask: true,
                message: '正在上传...'
              });
              var formData = new FormData();
              formData.append("uploadFile", file.file);
              uploadFile(formData).then(res=>{
                this.fileName = file.file.name;
                console.log(res);
                this.datas.attachmentId = res.fileId;
                Toast.clear();
              })
            },
            onConfirm(value){
                this.$set(this.datas,this.pickerName,value);
                 this.pickershow = false;
            },
            saveClock(){
              Toast.loading({
                duration: 0,   // 持续展示 toast
                mask: true,
                message: '正在提交...'
              });
                var saveData =  this.datas;
                saveData.applyUser = getStore('user_id');
                saveData.app_userId = getStore('user_id');
                saveData.CHECK_TIME = new Date().getTime().toString();
                saveData.CHECK_SIGN = this.$md5(saveData.app_userId + saveData.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");

              var data = {};
              data.app_userId = getStore('user_id');
              data.processRespondentUserId = getStore('user_id');
//                    if(this.orgType.indexOf("商业")>-1){
//                      data.day = "4";
//                    }else{
//                      data.day = "1";
//                    }
              data.day = "1";
              data.processType = "KQ";
              data.CHECK_TIME = new Date().getTime();
              data.CHECK_SIGN = this.$md5(data.app_userId + data.CHECK_TIME +"3a28fba00c015e0df7f88556c91cb7fc");
              data.startManagerId = this.startManagerId;
              data.bussinessTable = this.bussinessTable;
              data.processDefinitionKey = 'supplementCardProcess';
              console.log(data);

              // 判断是否可以提交
              canSave(data).then(res=>{
                if(res.code == 200){
                  if(!res.data.flag){
                    Toast.clear();
                    Dialog.confirm({
                      title: '提示',
                      message: res.data.message
                    }).then(() => {
                      saveActForgetClock(saveData).then(res=>{
                        if(res.code == 200){
                          data.bussinessIds = res.data;
                          processInstance(data).then(res1=>{
                            if(res1.code == 200){
                              Toast.clear();
                              Dialog.alert({
                                title: '提示',
                                message: '提交审批成功'
                              }).then(() => {
                                this.$router.go(-1);
                              });
                            }
                          })

                        }else if(res.code==400){
                          Toast.clear();
                          Dialog.alert({
                            title: '提示',
                            message: res.msg
                          })
                        }else if(res.code==300){
                          Dialog.alert({
                            title: '提示',
                            message: res.msg
                          })
                        }
                      })
                    }).catch(() => {
                      return false;
                    });
                  }else{
                    saveActForgetClock(saveData).then(res=>{
                      if(res.code == 200){
                        data.bussinessIds = res.data;
                        processInstance(data).then(res1=>{
                          if(res1.code == 200){
                            Toast.clear();
                            Dialog.alert({
                              title: '提示',
                              message: '提交审批成功'
                            }).then(() => {
                              this.$router.go(-1);
                            });
                          }
                        })

                      }else if(res.code==400){
                        Toast.clear();
                        Dialog.alert({
                          title: '提示',
                          message: res.msg
                        })
                      }else if(res.code==300){
                        Dialog.alert({
                          title: '提示',
                          message: res.msg
                        })
                      }
                    })
                  }
                }else if(res.code==400){
                  Toast.clear();
                  Dialog.alert({
                    title: '提示',
                    message: res.msg
                  })
                }
              })


            },
            cancel() {
                this.show = false;
            },
            onCancel(){
                this.pickershow = false;
            },
            showdatepicker(event,name){
                this.show = true;
                this.pickerName = name;
            },
            showpicker(event,name,title){
                this.pickershow = true;
                this.pickerName = name;
                this.title = title;
            },
            radioClick(value){
                this.datas.cardType = value;
              if(this.datas.cardType!=''&& this.datas.suppDate!=''){
                this.getWorkTimes();
              }
            },
            beforeRead(file) {
              var imgType = file.type.toLocaleLowerCase();
              if (imgType != 'image/jpg'&&imgType != 'image/jpeg'&&imgType != 'image/png'&&imgType != 'image/gif'&&imgType != 'image/bmp') {
                Toast('请上传图片');
                return false;
              }

              return true;
            }
        }
    }

</script>

<style lang="scss" scoped>
    @import '../../assets/style/mixin';
    @import '../../assets/style/public';
    .formContain{
        background: #fff;
        ul{
            padding: 0 .54rem;
            .flex{
                justify-content: space-between;
                align-items: center;
                line-height: 2rem;
                border-bottom: 1px solid #e0e0e0;
                padding: 0 .3rem;
                &:nth-last-of-type(1){
                    border: none;
                }
                .label{
                    font-size: .56rem;
                    line-height: 2rem;
                    color: #333333;
                }
                div{
                    display: flex;
                    justify-content: flex-end;
                    align-items: center;
                    position: relative;
                    flex-grow: 1;
                    img{
                        width: .68rem;
                        height: .64rem;
                    }
                    input{
                        color: #323233;
                        text-align: right;
                        font-size: .48rem;
                        padding-right: .48rem;
                        border: none;
                        &:-ms-input-placeholder{
                             color: #aaa;
                             font-size: .48rem;

                        }
                    }
                    .file{
                        width: 2rem;
                        height: 1rem;
                        font-size: 10rem;
                        opacity: 0;
                        position: absolute;
                        right: 0;
                    }
                    .van-cell{
                        font-size: .48rem;
                        padding-right: .48rem;
                        padding: 0;
                        color: #aaa;
                        .van-field__control{
                            color: #aaa;
                        }
                    }

                }
            }
        }
    }
    .van-radio__icon {
      width: 9rem;
    }
    .foot-button{
        margin-top: 2rem;
        margin-bottom: .4rem;
        .cancel-btn{
            margin-right: 1rem;
        }
    }
    #datetime-picker,#picker{
        position: fixed;
        width: 100%;
        bottom: 0;
    }
    .require{
      color: red;
      margin-right: 4px;
      font-style: inherit;
      font-size: 14px;
    }

</style>
